package ar.edu.itba.bundundu;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.client.http.AuthSubUtil;

import ar.edu.itba.bundundu.dao.UserDao;
import ar.edu.itba.bundundu.domain.UserIds;

@SuppressWarnings("serial")
public class IndexServlet extends HttpServlet {

	  @Override
	  public void doGet(HttpServletRequest req, HttpServletResponse resp)
	      throws IOException {
	  }

	  @Override
	  public void doPost(HttpServletRequest req, HttpServletResponse resp)
	      throws ServletException, IOException {
		  
		  RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
		  ResourceBundle rs = ResourceBundle.getBundle("config");
		  String f_key = rs.getString("application.key");
   

		  String param=req.getParameter("fb_sig_user");
		  if(param!=null){
			  Long uid= Long.valueOf(param);
			  UserIds user= UserDao.getUser(uid);			    
		  
			  if(user==null){
				  
				  String url = rs.getString("application.url");
				  String requestUrl = AuthSubUtil.getRequestUrl(url + "retrievetoken",
						  "http://www.google.com/calendar/feeds/", false, true);
				  
				  req.setAttribute("login", false);
				  req.setAttribute("requestUrl", requestUrl);
				  
			  }else{
				  req.setAttribute("login", true);
			  }

		  }
		  req.setAttribute("f_key", f_key);
		  dispatcher.forward(req, resp);
	  }
}
